const express = require("express");
const router = express.Router();
const classDB = require("../../schemas/class");
const liveDB = require("../../schemas/live");

//班期
router.get("/classList", async (req, res) => {
  const { pageSize, currentPage, className, enrollTime } = req.query;

  // 深度取值
  let data = await classDB.find().populate("liveList");
  // console.log(data);
  if (enrollTime) {
    const startTime = new Date(enrollTime[0]) / 1;
    const endTime = new Date(enrollTime[1]) / 1;

    // console.log(startTime);
    data = data.filter((item) => {

      console.log(startTime,item.enrollTime[0]);
      return startTime <= item.enrollTime[0] && endTime >= item.enrollTime[1];
    });

    // console.log(data);
  }

  if (className) {
    data = data.filter((item) => item.className.includes(className));
  }
  const total = data.length;
  data = data.splice((currentPage - 1) * pageSize, pageSize);

  // console.log(data);
  res.send({
    code: 0,
    total,
    data,
  });
});

module.exports = router;
